{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# *和**做拆包使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-24T13:25:18.482600Z",
     "start_time": "2021-11-24T13:25:18.469600Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "拆掉list容器\n",
      "[1, 2, 3, 4, 5]\n",
      "1 2 3 4 5\n",
      "\n",
      "\n",
      "拆掉元组容器\n",
      "(1, 2, 3, 4, 5)\n",
      "1 2 3 4 5\n",
      "\n",
      "\n",
      "拆掉range\n",
      "range(1, 6)\n",
      "1 2 3 4 5\n",
      "\n",
      "\n",
      "拆掉字典容器\n",
      "{'a': 1, 'b': 2, 'c': 3}\n",
      "a b c\n"
     ]
    }
   ],
   "source": [
    "# *号可以拆掉list或者元组的容器外壳\n",
    "print('拆掉list容器')\n",
    "print([1,2,3,4,5])\n",
    "print(*[1,2,3,4,5])\n",
    "print('\\n')\n",
    "\n",
    "print('拆掉元组容器')\n",
    "print((1,2,3,4,5))\n",
    "print(*(1,2,3,4,5))\n",
    "print('\\n')\n",
    "\n",
    "print('拆掉range')\n",
    "print(range(1,6))\n",
    "print(*range(1,6))\n",
    "print('\\n')\n",
    "\n",
    "# **可以拆掉字典容器\n",
    "print('拆掉字典容器')\n",
    "print({'a':1,'b':2,'c':3})\n",
    "print(*{'a':1,'b':2,'c':3})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# *接收多个参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-24T13:30:26.218801Z",
     "start_time": "2021-11-24T13:30:26.206801Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[3, 4, 5]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a,b,*c = [1,2,3,4,5]\n",
    "a\n",
    "b\n",
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# * 和 ** 函数传参拆包 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-24T13:34:15.832202Z",
     "start_time": "2021-11-24T13:34:15.825202Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "def func1(x,y):\n",
    "    print(x+y)\n",
    "\n",
    "func1(*[1,2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-24T13:35:32.136602Z",
     "start_time": "2021-11-24T13:35:32.128602Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "def func2(data1 = 0,data2 = 0):\n",
    "    print(data1 + data2)\n",
    "    \n",
    "dict1 = {'data1': 1, 'data2': 2}\n",
    "func2(**dict1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 函数接收多个参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## * 接收不定长的多个参数,并打包成元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-24T13:43:09.328003Z",
     "start_time": "2021-11-24T13:43:09.319003Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n",
      "arges:  (1, 2, 3, 4, 5)\n"
     ]
    }
   ],
   "source": [
    "def func1(*arges):\n",
    "    print(type(arges))\n",
    "    print('arges: ',arges)\n",
    "\n",
    "func1(1,2,3,4,5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ** 接收不定长的多个键值对，并打包成字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-24T13:43:12.998803Z",
     "start_time": "2021-11-24T13:43:12.990803Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "kwarge:  {'a': 1, 'b': 2, 'c': 3}\n"
     ]
    }
   ],
   "source": [
    "def func2(**kwarge):\n",
    "    print(type(kwarge))\n",
    "    print('kwarge: ',kwarge)\n",
    "\n",
    "func2(a=1,b=2,c=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## *，** 和普通参数可以同时使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-24T13:42:13.601603Z",
     "start_time": "2021-11-24T13:42:13.592603Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  1\n",
      "b:  2\n",
      "arges:  (3, 4, 5, 6)\n",
      "kwarge:  {'x': 1, 'y': 2, 'z': 3}\n"
     ]
    }
   ],
   "source": [
    "def func3(a,b,*arges,**kwarge):\n",
    "    print('a: ',a)\n",
    "    print('b: ',b)\n",
    "    print('arges: ',arges)\n",
    "    print('kwarge: ',kwarge)\n",
    "func3(1,2,3,4,5,6,x=1,y=2,z=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
